package com.syp.envops.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.syp.envops.domain.vo.ServerVo;
import com.syp.envops.service.ServerServiceI;
import com.syp.envops.util.ProcessResult;

@Controller
@RequestMapping("/server")
public class ServerController {
	
	private static final Logger logger = LoggerFactory.getLogger(ServerController.class);

	@Autowired
	private ServerServiceI serverService;
	
	@RequestMapping("/get")
	@ResponseBody
	public ProcessResult get(Integer sid) {
		ProcessResult pr = new ProcessResult();
		try {
			ServerVo server = serverService.get(sid);
			pr.setSuccess(true);
			pr.setMessage("服务器查询成功。");
			pr.getRet().put("server", server);

		} catch (Exception e) {
			logger.error("服务器查询失败:", e);
			
			pr.setSuccess(false);
			pr.setMessage("服务器查询失败。");
		}
		return pr;
	}

	@RequestMapping("/list.tpl")
	public String listTpl() {
		return "/server/list";
	}

	@RequestMapping("/list")
	@ResponseBody
	public ProcessResult list() {
		ProcessResult pr = new ProcessResult();
		try {
			List<ServerVo> serverList = serverService.list();
			pr.setSuccess(true);
			pr.setMessage("服务器列表查询成功。");
			pr.getRet().put("serverList", serverList);

		} catch (Exception e) {
			logger.error("服务器列表查询失败:", e);
			
			pr.setSuccess(false);
			pr.setMessage("服务器列表查询失败。");
		}
		return pr;
	}
	
	@RequestMapping("/add.dialog")
	public String addTpl() {
		return "/server/add.dialog";
	}

	@RequestMapping("dadd")
	@ResponseBody
	public ProcessResult dadd(@RequestBody ServerVo server) {
		ProcessResult pr = new ProcessResult();
		try {
			serverService.insert(server);
			pr.setSuccess(true);
			pr.setMessage("添加服务器成功。");
			pr.getRet().put("server", server);

		} catch (Exception e) {
			logger.error("添加服务器失败:", e);
			
			pr.setSuccess(false);
			pr.setMessage("添加服务器失败。");
		}
		return pr;
	}

	@RequestMapping("ddel")
	@ResponseBody
	public ProcessResult ddel(Integer sid) {
		ProcessResult pr = new ProcessResult();
		try {
			serverService.delete(sid);
			pr.setSuccess(true);
			pr.setMessage("删除服务器成功。");

		} catch (Exception e) {
			logger.error("删除服务器失败:", e);
			
			pr.setSuccess(false);
			pr.setMessage("删除服务器失败。");
		}
		return pr;
	}
	
	@RequestMapping("/edit.dialog")
	public String editTpl() {
		return "/server/edit.dialog";
	}
	
	@RequestMapping("dedit")
	@ResponseBody
	public ProcessResult dedit(@RequestBody ServerVo server) {
		ProcessResult pr = new ProcessResult();
		try {
			serverService.edit(server);
			pr.setSuccess(true);
			pr.setMessage("编辑服务器成功。");

		} catch (Exception e) {
			logger.error("编辑服务器失败:", e);
			
			pr.setSuccess(false);
			pr.setMessage("编辑服务器失败。");
		}
		return pr;
	}

}
